﻿namespace RuoVea.Fams.Web.Controllers;

[Authorize(Roles = "None")]
public partial class RecordsController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly RecordsService _recordsService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="recordsService"></param> 
    public RecordsController(RecordsService recordsService)
    {
        _recordsService = recordsService;
    }

    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("records/pages")]
    public async Task<RestfulResult<PageResult<FamsRecords>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        RecordsPageParam? data = JsonConvert.DeserializeObject<RecordsPageParam>(dataStr);
        data ??= new RecordsPageParam { PageNo = 1, PageSize = 10 };

        var query = await _recordsService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有
    /// </summary>
    /// <returns></returns>
    [HttpPost("records/list")]
    public async Task<RestfulResult<PageResult<FamsRecords>>> GetlistAsync()
    {
        var restful = await _recordsService.GetlistAsync();
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("records/add")]
    public async Task<RestfulResult> AddAsync([FromBody] FamsRecords data)
    {
        var restful = await _recordsService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("records/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] FamsRecords data)
    {
        var restful = await _recordsService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("records/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        var restful = await _recordsService.DeleteAsync(data);
        return restful;
    }
}